package com.qf.flashlib.net;

import android.app.Application;
import android.content.IntentFilter;

import com.qf.flashlib.app.BaseApp;
import com.qf.flashlib.config.Constants;

/**
 * 2019/3/27 14:30
 */
public class NetManager {

    private NetWorkReceiver mReceiver;

    private Application application;

    private boolean isRegister; //判断广播是否已经注册

    private NetManager() {
        mReceiver = new NetWorkReceiver();
    }

    private static class NetHolder {
        private static NetManager manager = new NetManager();
    }


    public static NetManager getInstance() {
        return NetHolder.manager;
    }


    public void init() {
        this.application = BaseApp.getInstance();
        if (application == null)
            return;
        //广播已经注册
        if (isRegister)
            return;
        //动态注册广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.ANDROID_NET_CHANGE_ACTION);
        this.application.registerReceiver(mReceiver, filter);
        isRegister = true;
    }

    public void registerObserver(Object target) {
        mReceiver.registerObserver(target);
    }

    public void unRegisterObserver(Object target) {
        mReceiver.unRegisterObserver(target);
    }

    public void unRegisterAllObserver() {
        mReceiver.unRegisterAllObserver();
        application.unregisterReceiver(mReceiver);
        isRegister = false;
    }


}
